package com.twang.stock.task.old;

import com.twang.stock.entity.*;
import com.twang.stock.mapper.*;
import com.twang.stock.enmu.HotTagEnum;
import com.twang.stock.enmu.UserBusinessTypeEnum;
import com.twang.stock.service.UserService;
import com.twang.stock.service.jinyan.HolidayService;
import com.twang.stock.service.jinyan.UserBusinessTypeService;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class QinXuGaoDuTask extends BaseTask {

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;
    @Resource
    private HotTagMapper hotTagMapper;
    @Resource
    private StockTagMapper stockTagMapper;
    @Autowired
    private HolidayService holidayService;

    @Autowired
    @Lazy
    private UserBusinessTypeService userBusinessTypeService;

    @Autowired
    private UserService  userService;

   // @Scheduled(cron = "0 05 15 * * ?")
    public void excute() {
        if (!isWorkTradeTime()) {
            return;
        }
    }


    /**
     * 生成板块高度
     *
     * @param stockInfoList
     */
    public void initGaiNianBanKuai(List<StockInfo> stockInfoList, String gainian, String banKuaiCode) {
        HotTag hotTag = new HotTag();
        hotTag.setHotTag(HotTagEnum.Core_6.getCode());
        HotTag hotTag1 = hotTagMapper.selectOne(hotTag);
        Long id;
        if (null == hotTag1) {
            hotTag.setCreateTime(new Date());
            hotTag.setUpdateTime(new Date());
            hotTagMapper.insert(hotTag);
            HotTag param = new HotTag();
            param.setHotTag(hotTag.getHotTag());
            hotTag = hotTagMapper.selectOne(param);
            id = hotTag.getId();
        } else {
            id = hotTag1.getId();
        }
        List<StockInfo> resultList = stockInfoList.stream().sorted(Comparator.comparing(StockInfo::getZhangdie).reversed()).collect(Collectors.toList());
        int size = resultList.size();
        for (int i = 0; i < size; i++) {
            insert(resultList.get(i), id, gainian, banKuaiCode);
        }
    }

    private void insert(StockInfo stockInfo, Long id, String gainian, String banKuaiCode) {
        if (null != stockInfo.getZhangdie() && stockInfo.getZhangdie().compareTo(new BigDecimal("9.9")) == 1) {
            CompanyCodeInfo param1 = new CompanyCodeInfo();
            param1.setCompanyCode(stockInfo.getCode());
            CompanyCodeInfo result = companyCodeInfoMapper.selectOne(param1);
            if (null == result) {
                return;
            }
            StockTag stockTag = new StockTag();
            stockTag.setTagId(id);
            stockTag.setStockId(result.getId());
            StockTag record = stockTagMapper.selectOne(stockTag);
            if (null == record) {
                stockTag.setHangYe(gainian);
                stockTag.setCreateTime(new Date());
                stockTag.setUpdateTime(new Date());
                stockTagMapper.insert(stockTag);
            }
        }
    }

    /**
     *
     */
    public Long initChangXianChi() {
        User user = userService.getUserByToken();
        HotTag hotTag = new HotTag();
        UserBusinessType userBusinessType = userBusinessTypeService.getUserBusinessType(user.getId(), UserBusinessTypeEnum.TYPE_HOT_TAG.getCode());
        HotTag usertag = hotTagMapper.selectByPrimaryKey(userBusinessType.getBusinessId());
        //
        hotTag.setHotTag(HotTagEnum.CORE_DUAN_XIAN_BAO.getCode() + "_" + holidayService.getDayByNum(0));
        hotTag.setUserId(user.getId());
        HotTag hotTag1 = hotTagMapper.selectOne(hotTag);
        Long id;
        if (null == usertag && null == hotTag1) {
            hotTag.setCreateTime(new Date());
            hotTag.setUpdateTime(new Date());
            hotTag.setHotTemperatureName(HolidayCopy.getToday());
            hotTag.setRemark("短线宝");
            hotTag.setUserId(user.getId());
            hotTagMapper.insert(hotTag);
            HotTag param = new HotTag();
            param.setHotTag(hotTag.getHotTag());
            param.setUserId(user.getId());
            hotTag = hotTagMapper.selectOne(param);
            id = hotTag.getId();
        } else {
            if(null==hotTag1){
                id = hotTag.getId();
            }else {
                id = hotTag1.getId();
            }
        }
        return id;
    }
}
